#include "widget.h"
#include "ui_widget.h"

#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include "driver_si7006.h"


Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    ui->setupUi(this);

    QPixmap pixmapled(":/icon/LED_close.png");
    pixmapled = pixmapled.scaled(100, 100, Qt::KeepAspectRatio);
    ui->LED1->setIcon(QIcon(pixmapled));

    pixmapled.load(":/icon/LED_close.png");
    pixmapled = pixmapled.scaled(100, 100, Qt::KeepAspectRatio);
    ui->LED2->setIcon(QIcon(pixmapled));

    pixmapled.load(":/icon/LED_close.png");
    pixmapled = pixmapled.scaled(100, 100, Qt::KeepAspectRatio);
    ui->LED3->setIcon(QIcon(pixmapled));

    QPixmap pixmapir(":/icon/ir.png");
    pixmapir = pixmapir.scaled(100, 100, Qt::KeepAspectRatio);
    ui->label_ir->setPixmap(pixmapir);
    ui->label_ir_2->setText("Infrared");

    QPixmap pixmapals(":/icon/als.png");
    pixmapals = pixmapals.scaled(100, 100, Qt::KeepAspectRatio);
    ui->label_als->setPixmap(pixmapals);
    ui->label_als_2->setText("Light");

    QPixmap pixmapps(":/icon/ps.png");
    pixmapps = pixmapps.scaled(100, 100, Qt::KeepAspectRatio);
    ui->label_ps->setPixmap(pixmapps);
    ui->label_ps_2->setText("Approach");

    QPixmap pixmaptemp(":/icon/temp.png");
    pixmaptemp = pixmaptemp.scaled(100, 100, Qt::KeepAspectRatio);
    ui->label_temp->setPixmap(pixmaptemp);
    ui->label_temp_2->setText("Temp");

    QPixmap pixmaphumi(":/icon/humi.png");
    pixmaphumi = pixmaphumi.scaled(100, 100, Qt::KeepAspectRatio);
    ui->label_humi->setPixmap(pixmaphumi);
    ui->label_humi_2->setText("Humi");

    led1_starus = 0;
    led2_starus = 0;
    led3_starus = 0;

    get_env_status();

    ui->label_ir_3->setText(QString::number(ap3216_ir));
    ui->label_als_3->setText(QString::number(ap3216_als));
    ui->label_ps_3->setText(QString::number(ap3216_ps));
    ui->label_humi_3->setText(QString::number(humi_val));
    ui->label_temp_3->setText(QString::number(temp_val));

    //start timer
    timer = new QTimer;
    connect(timer, &QTimer::timeout, this, &Widget::timer_out_handle);

    timer->start(1000);

}

Widget::~Widget()
{
    delete ui;
}

int Widget::get_env_status()
{
    led_fd = open("/dev/led", O_RDWR);
    if(led_fd == -1)
    {
        QMessageBox::information(nullptr, "Wring", "open /dev/led error");
        exit(-1);
    }

    si7006_fd = open("/dev/tem_hum", O_RDWR);
    if(si7006_fd == -1)
    {
        QMessageBox::information(nullptr, "Wring", "open /dev/tem_hum error");
        exit(-1);
    }

//    ap3216_fd = open("/dev/myap3216", O_RDWR);
//    if(ap3216_fd == -1)
//    {
//        QMessageBox::information(nullptr, "Wring", "open /dev/myap3216 error");
//        exit(-1);
//    }

    read(led_fd, led_status_buf, sizeof(led_status_buf));
    led1_starus = led_status_buf[0] - '0';
    led2_starus = led_status_buf[1] - '0';
    led3_starus = led_status_buf[2] - '0';

    int ledval;
    ioctl(led_fd, _IOR('l',2,int), &ledval);
    (ledval & 0x1) == 0 ? led1_starus = 0 : led1_starus = 1;
    (ledval & 0x2) == 0 ? led2_starus = 0 : led2_starus = 1;
    (ledval & 0x4) == 0 ? led3_starus = 0 : led3_starus = 1;

    int temp_code, humi_code;
    ioctl(si7006_fd, GET_TMP, &temp_code);
    ioctl(si7006_fd, GET_HUM, &humi_code);

    temp_val = (175.72 * temp_code) / 65536 - 46.85;
    humi_val = (125.0 * humi_code) / 65536 - 6;

    //read(ap3216_fd, ap3216val, sizeof(ap3216val));
    ap3216_ir = ap3216val[0];
    ap3216_als = ap3216val[1];
    ap3216_ps = ap3216val[2];

    return 0;
}

void Widget::timer_out_handle()
{
    //update device env status
    update_env_status();
}

int Widget::update_env_status()
{
    int ledval;
    ioctl(led_fd, _IOR('l',2,int), &ledval);
    (ledval & 0x1) == 0 ? led1_starus = 0 : led1_starus = 1;
    (ledval & 0x2) == 0 ? led2_starus = 0 : led2_starus = 1;
    (ledval & 0x4) == 0 ? led3_starus = 0 : led3_starus = 1;

    int temp_code, humi_code;
    ioctl(si7006_fd, GET_TMP, &temp_code);
    ioctl(si7006_fd, GET_HUM, &humi_code);

    temp_val = (175.72 * temp_code) / 65536 - 46.85;
    humi_val = (125.0 * humi_code) / 65536 - 6;

//    read(ap3216_fd, ap3216val, sizeof(ap3216val));
//    ap3216_ir = ap3216val[0];
//    ap3216_als = ap3216val[1];
//    ap3216_ps = ap3216val[2];

    //update env status
//    ui->label_ir_3->setText(QString::number(ap3216_ir));
//    ui->label_als_3->setText(QString::number(ap3216_als));
//    ui->label_ps_3->setText(QString::number(ap3216_ps));
    ui->label_humi_3->setText(QString::number(humi_val));
    ui->label_temp_3->setText(QString::number(temp_val));

    //
    QPixmap pixmapled;
    if(led1_starus == 1)
        pixmapled.load(":/icon/LED_open.png");
    else if(led1_starus == 0)
        pixmapled.load(":/icon/LED_close.png");
    pixmapled = pixmapled.scaled(100, 100, Qt::KeepAspectRatio);
    ui->LED1->setIcon(QIcon(pixmapled));

    if(led2_starus == 1)
        pixmapled.load(":/icon/LED_open.png");
    else if(led2_starus == 0)
        pixmapled.load(":/icon/LED_close.png");
    pixmapled = pixmapled.scaled(100, 100, Qt::KeepAspectRatio);
    ui->LED2->setIcon(QIcon(pixmapled));

    if(led3_starus == 1)
        pixmapled.load(":/icon/LED_open.png");
    else if(led3_starus == 0)
        pixmapled.load(":/icon/LED_close.png");
    pixmapled = pixmapled.scaled(100, 100, Qt::KeepAspectRatio);
    ui->LED3->setIcon(QIcon(pixmapled));

    return 0;
}

void Widget::led_state(int ledfd,int witch,int lednum,int state)
{
    int data[2] = {witch,lednum};
    switch(state){
    case 1:
        ioctl(ledfd,_IOW('l',1,int [2]),data);
        break;
    case 0:
        ioctl(ledfd,_IOW('l',0,int [2]),data);
        break;
    }
}

void Widget::on_LED1_clicked()
{
    QPixmap pixmapled;
    if(led1_starus == 0)
    {
        pixmapled.load(":/icon/LED_open.png");
        led1_starus = 1;
        led_status_buf[0] = '1';
        led_state(led_fd, 1, 0, 1);
    }
    else if(led1_starus == 1)
    {
        pixmapled.load(":/icon/LED_close.png");
        led1_starus = 0;
        led_state(led_fd, 1, 0, 0);
    }

    pixmapled = pixmapled.scaled(100, 100, Qt::KeepAspectRatio);
    ui->LED1->setIcon(QIcon(pixmapled));
}

void Widget::on_LED2_clicked()
{
    QPixmap pixmapled;
    if(led2_starus == 0)
    {
        pixmapled.load(":/icon/LED_open.png");
        led2_starus = 1;
        led_state(led_fd, 1, 1, 1);
    }
    else if(led2_starus == 1)
    {
        pixmapled.load(":/icon/LED_close.png");
        led2_starus = 0;
        led_state(led_fd, 1, 1, 0);
    }

    pixmapled = pixmapled.scaled(100, 100, Qt::KeepAspectRatio);
    ui->LED2->setIcon(QIcon(pixmapled));
}

void Widget::on_LED3_clicked()
{
    QPixmap pixmapled;
    if(led3_starus == 0)
    {
        pixmapled.load(":/icon/LED_open.png");
        led3_starus = 1;
        led_state(led_fd, 1, 2, 1);
    }
    else if(led3_starus == 1)
    {
        pixmapled.load(":/icon/LED_close.png");
        led3_starus = 0;
        led_state(led_fd, 1, 2, 0);
    }

    pixmapled = pixmapled.scaled(100, 100, Qt::KeepAspectRatio);
    ui->LED3->setIcon(QIcon(pixmapled));
}
